package com.woniu.admin.config;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request,
                                         HttpServletResponse response,
                                         Object handler,
                                         Exception e) {

        ModelAndView mav = new ModelAndView();

        System.out.println("出异常的方法：" + handler);
        System.out.println("异常类对象：" + e);
        System.out.println("异常信息：" + e.getMessage());

        if (e instanceof NullPointerException) {
            mav.addObject("errorMsg", "空指针了，你怎么写的代码？？？");

        } else if (e instanceof NumberFormatException) {
            mav.addObject("errorMsg", "你把非数字转换成了数字！");
            mav.addObject("errorMsg", e.getStackTrace()[0].toString());

        } else {
            mav.addObject("errorMsg", e.getMessage());
            mav.addObject("errorMsg", "未知程序出错，请找管理员！");
        }
//         mav.setViewName("forward:/error.html");
        mav.setViewName("error.html");

        return mav;
    }
}